<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="800" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="FormData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="130px">
			<el-form-item label="任务名称" prop="name">
				<el-input v-model="FormData.name" style="width:56%" clearable placeholder="请输入任务名称"></el-input>
			</el-form-item>
			<el-form-item label="考评门店" prop="stores">
				<Store v-model="FormData.stores" multiple :defaultValue="FormData.stores" style="width:56%"></Store>
			</el-form-item>
			<el-form-item label="任务有效期" prop="date">
				<div style="width:56%">
					<el-date-picker v-model="FormData.date" type="daterange" :disabled-date="disabledDate"
						range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" @change="changeDate"/>
				</div>
			</el-form-item>
			<el-form-item label="执行频次" prop="frequency">
				<el-radio-group v-model="FormData.frequency">
					<el-radio :label="1">每天</el-radio>
					<el-radio :label="7">每周</el-radio>
					<el-radio :label="30">每月</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="巡查模板" prop="template">
				<el-select v-model="FormData.template" placeholder="请选择巡查等级配置">
					<el-option v-for="(item,index) in unitList" :key=" index" :value="item.id" :label="item.name" />
				</el-select>
			</el-form-item>
			<el-form-item label="考评方式" prop="type">
				<el-radio-group v-model="FormData.type">
					<el-radio label="线下巡查">线下巡查</el-radio>
					<el-radio label="线上巡查">线上巡查</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="考评人" prop="user">
				<Users v-model="FormData.user"></Users>
			</el-form-item>
			<el-form-item label="执行时效(天)" prop="ageing">
				<el-input v-model="FormData.ageing" style="width:56%"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				fileurlArr: [],
				//表单数据
				FormData: {
					stores: [],
					taskStart:null,
					taskEnd:null,
					user:{
						id:null
					}
				},
				levelList: [],
				//验证规则
				rules: {
					name: [{
						required: true,
						message: '请输入任务名称'
					}],
					stores: [{
						required: true,
						message: '请选择所属门店'
					}],
					level: [{
						required: true,
						message: '请选择巡查等级配置'
					}],

				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},

				unitList: [],
				userInfo: {}
			}
		},
		watch: {

		},
		created() {
			this.userInfo = this.$TOOL.data.get("USER_INFO")
		},
		mounted() {
			this.getUnit()
		},
		methods: {
			changeDate(e){
				console.log(e)
				this.FormData.taskStart = e[0]
				this.FormData.taskEnd = e[1]
			},
			// 禁止选择当前日期之前的
			disabledDate(time) {
				return time.getTime() < Date.now() - 8.64e7
			},
			//新增等级
			addLevel() {
				if (this.levelList.length == 9) {
					this.$message('最多只能新增9级！')
				} else {
					this.levelList.push({
						user: []
					})
				}
				console.log(1233, this.levelList)
			},
			// 删除等级
			deleteLevel(item, index) {
				this.levelList.splice(index, 1)
			},
			async getUnit() {
				let data = {
					page: 1,
					pagesize: 100
				}
				const res = await this.$API.api.level.list.post(data)
				if (res.data.datas) {
					this.unitList = res.data.datas
				}
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true
				let stores = []
				if (this.FormData.stores.length) {
					this.FormData.stores.forEach(item => {
						stores.push({
							id: item.id
						})
					})
				} else {
					stores = null
				}
				let data = {
					frequency: Number(this.FormData.frequency),
					level: this.FormData.level,
					name: this.FormData.name,
					taskStart: this.FormData.taskStart,
					taskEnd: this.FormData.taskEnd,
					stores: stores,
					user:{id:this.FormData.user.id},
					template: this.FormData.template,
					ageing: Number(this.FormData.ageing),
					type: this.FormData.type,
				}
				if(this.FormData.id){
					data.id = this.FormData.id
				}

				const res = await this.$API.api.ptask.save.post(data)
				this.isSaveing = false;
				if (res.code == 200) {
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				this.FormData.id = data.id
				this.FormData.name = data.name
				this.FormData.stores = data.stores
				this.FormData.user = data.user
				this.FormData.ageing = data.ageing
				this.FormData.frequency = data.frequency
				this.FormData.template = data.template
				this.FormData.type = data.type
				this.FormData.taskStart = data.taskStart
				this.FormData.taskEnd = data.taskEnd
				this.FormData.date = [this.FormData.taskStart,this.FormData.taskEnd]
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.FormData, data)
			}
		}
	}
</script>

<style>
</style>
